package com.hkfrich.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkfrich.common.constant.Constants;
import com.hkfrich.common.utils.SecurityUtils;
import com.hkfrich.generator.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import com.hkfrich.common.controller.BaseController;
import com.hkfrich.common.utils.ConvertUtils;
import com.hkfrich.common.utils.ResultUtils;
import com.hkfrich.system.domain.SysDictType;
import com.hkfrich.system.service.SysDictTypeService;
//import com.hkfrich.common.core.page.TableDataInfo;

/**
 * @ClassName 字典类型Controller
 * @Desscription
 * @Author _Heishuai
 * @Date 2022-09-15 00:22:11
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/dicttype")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 通过条件查询单个字典类型 不分页
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public ResultUtils getOne(SysDictType sysDictType)
    {
        QueryWrapper<SysDictType> query = initBaseQueryWrapper(sysDictType);
        SysDictType one = sysDictTypeService.getOne(query);
        return ResultUtils.success(one,"字典类型查询成功");
    }

    /**
     * 通过id查询单个字典类型
     */
    @RequestMapping("/getOneById")
    @ResponseBody
    public ResultUtils getOneById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        SysDictType one = sysDictTypeService.getById(id);
        return ResultUtils.success(one,"字典类型查询成功");
    }

    /**
     * 查询字典类型列表  分页
     */
    @RequestMapping("/listAll")
    @ResponseBody
    public ResultUtils listAll()
    {
        List<SysDictType> list = sysDictTypeService.list();
        return ResultUtils.success(list,"字典类型查询成功");
    }

    /**
     * 查询字典类型列表  分页
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultUtils list(@RequestBody SysDictType sysDictType)
    {
        Page<SysDictType> page = initBaseQueryPageWrapper(sysDictType);
        QueryWrapper<SysDictType> query = initBaseQueryWrapper(sysDictType);
        Page<SysDictType> pageGrid = sysDictTypeService.page(page, query);
        return ResultUtils.success(pageGrid,"用户列表查询成功");
    }

    /**
     * 新增字典类型
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultUtils save(@RequestBody SysDictType sysDictType)
    {
        if (Constants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictType)))
        {
            return ResultUtils.fail("新增字典'" + sysDictType.getDictName() + "'失败，字典类型已存在");
        }
        sysDictType.setCreateBy(SecurityUtils.getUserId());
        sysDictTypeService.save(sysDictType);
        //更新缓存
        DictUtils.setDictCache(sysDictType.getDictType(), null);
        return ResultUtils.success("字典类型保存成功");
    }

    /**
     * 修改字典类型
     */
    @RequestMapping("/updateById")
    @ResponseBody
    public ResultUtils updateById(@RequestBody SysDictType sysDictType)
    {
        if (Constants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictType)))
        {
            return ResultUtils.fail("修改字典'" + sysDictType.getDictName() + "'失败，字典类型已存在");
        }
        sysDictType.setUpdateBy(SecurityUtils.getUserId());
        sysDictTypeService.updateDictType(sysDictType);
        return ResultUtils.success("字典类型修改成功");
    }

    /**
     * 删除字典类型
     */
    @RequestMapping("/removeById")
    @ResponseBody
    public ResultUtils removeById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        return ResultUtils.success(sysDictTypeService.removeById(id),"字典类型删除成功");
    }

    /**
     * 批量删除字典类型
     */
    @RequestMapping("/removeByIds")
    @ResponseBody
    public ResultUtils removeByIds(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        String ids = params.getString("ids");
        List<Long> list = ConvertUtils.toList(ids);
        sysDictTypeService.deleteDictTypeByIds(list);
        return ResultUtils.success("批量删除字典类型成功");
    }

    /**
     * 刷新字典缓存
     */
    @RequestMapping("/refreshCache")
    @ResponseBody
    public ResultUtils refreshCache()
    {
        sysDictTypeService.resetDictCache();
        return ResultUtils.success("刷新缓存成功");
    }


}
